 
// 请求配置文件
var Fly = require("./wx")
var fly = new Fly
let number=0
// fly.config.baseURL = "https://test.yookaclub.com"
fly.config.baseURL = "https://www.hxznzz.tech/api"
fly.config.timeout = 60000
//添加请求拦截器
fly.interceptors.request.use((config, promise) => {
  //全局设置请求头
  number++
  let conversionString = wx.getStorageSync('token') || ''
  let deviceType = wx.getStorageSync('deviceType') || ''
  let source ='mini'
  let version='6.10.1'
  let token = wx.getStorageSync('token') || ''
  let authorization = 'bearer ' + conversionString
  // config.headers.Authorization = authorization
  config.headers.deviceType = deviceType
  config.headers.source = source
  config.headers.version = version
  config.headers.hxToken = token
  
  if (config.url === 'mall/v1/order/addOrder') {
      wx.showLoading({
      title: '订单生成中...',
      mask: true,
    })
  }
 else if (config.url === 'mall/v1/payment/pay') {
    wx.showLoading({
    title: '正在支付中...',
    mask: true,
  })
}

else if (config.url == '/mall/v2/cart/listV2') {
  wx.showLoading({
  title: '加载中...',
  mask: true,
})
}
  return config;
})

//添加响应拦截器，响应拦截器会在then/catch处理之前执行
fly.interceptors.response.use(
  (res, promise) => {
    wx.hideLoading()

    // number--
    // if(number==0){
    //   wx.hideLoading()
    // }

    if (res.data.code === 30007) {
          wx.removeStorageSync('token')
          wx.navigateTo({
            url: '/packageD/pages/authorize/authorize',
          });
          return Promise.reject(response.data)
    }
    if (res.data.code !== 0) {
      if (res.data.code !== 201 && res.data.code !== 202) {
        setTimeout(function(){
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          });

        },100)
        return Promise.reject(response.data)
      }

    }
    
    //只将请求结果的data字段返回
    return res.data
  }, (err) => {
    wx.hideLoading()
    // return Promise.resolve()
  }
)
export default fly